package com.systemsplanet.maven.plugin;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.Enumeration;
import java.util.Properties;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;

import com.google.api.translate.Translate;

/**
 * generate Language files
 *
 * @goal localize
 *
 * @phase generate-sources
 */
public class LocalizeMojo extends AbstractMojo {

	/**
	 * Debug mode
	 *
	 * @parameter expression="false"
	 *
	 */
	private Boolean debug;

	/**
	 * English translation source path
	 *
	 * @parameter expression="${basedir}/src/main/resources/"
	 *
	 */
	private File sourceTranslationPath;

	/**
	 * English translation source file pattern
	 *
	 * @parameter expression="Language_{0}.properties"
	 *
	 */
	private String languageFilePattern;

	/**
	 * English translation source language
	 *
	 * @parameter expression="en"
	 *
	 */
	private String sourceLanguage;
	/**
	 * Destination directory for output files
	 *
	 * @parameter expression="${project.build.directory}/classes/"
	 */
	private File destinationPath;

	/*
	 * ARABIC = "ar"; CHINESE = "zh"; CHINESE_SIMPLIFIED = "zh-CN";
	 * CHINESE_TRADITIONAL = "zh-TW"; DUTCH = "nl"; FRENCH = "fr"; GERMAN =
	 * "de"; GREEK = "el"; ITALIAN = "it"; JAPANESE = "ja"; KOREAN = "ko";
	 * PORTUGESE = "pt"; RUSSIAN = "ru"; SPANISH = "es";
	 */

	/**
	 * list of languages
	 *
	 * @parameter expression="es,fr,nl,de,it"
	 */
	private String targetLanguages;

	@SuppressWarnings("unchecked")
	public void execute() throws MojoExecutionException, MojoFailureException {
		Object[] arguments = { sourceLanguage };
		String sourceTranslationFileName = MessageFormat.format(
				sourceTranslationPath + File.separator + languageFilePattern,
				arguments);
		File sourceTranslationFile = new File(sourceTranslationFileName);

		if (debug) {
			System.out.println("sourceTranslationPath: ["
					+ sourceTranslationPath + "]");
			System.out.println("languageFilePattern: [" + languageFilePattern
					+ "]");
			System.out.println("sourceLanguage: [" + sourceLanguage + "]");
			System.out.println("sourceTranslationFileName: ["
					+ sourceTranslationFileName + "]");
			System.out.println("sourceTranslationFile ["
					+ sourceTranslationFile.getAbsolutePath() + "]");
			System.out.println("destinationPath ["
					+ destinationPath.getAbsolutePath() + "]");
		}
		if (!destinationPath.exists())
			destinationPath.mkdirs();
		FileInputStream fis = null;
		BufferedWriter bw = null;
		try {
			String[] languages = targetLanguages.split(",");
			Properties p = new Properties();
			fis = new FileInputStream(sourceTranslationFile.getAbsolutePath());
			p.load(fis);
			fis.close();
			fis = null;
			for (String destLanguage : languages) {
				Object[] args = { destLanguage };
				String destTranslationFileName = MessageFormat.format(
						destinationPath + File.separator + languageFilePattern,
						args);
				System.out.print(destLanguage + " ");
				if (debug) {
					System.out.println("destTranslationFileName:"
							+ destTranslationFileName);
				}
				bw = new BufferedWriter(new FileWriter(destTranslationFileName));
				Enumeration msgKeys = p.keys();
				try {
					while (msgKeys.hasMoreElements()) {
						String msgKey = (String) msgKeys.nextElement();
						String msgVal = p.getProperty(msgKey);
						String[] msgVals = msgVal.split("<br>");
						if (debug) {
							System.out.println(msgKey + "=" + msgVal);
						} else {
							System.out.print(".");
						}
						bw.write(msgKey + "=");
						if (debug) {
							System.out.print(msgKey + "=");
						}
						int lines = 0;
						for (String msg : msgVals) {
							String translatedText = "";
							if (lines > 0) {
								bw.write("<br>\\\n");
							}
							lines++;
							if (msg != null && msg.length() > 0) {
								try {
									translatedText = Translate.translate(msg,
											sourceLanguage, destLanguage);
								} catch (Exception e) {
									translatedText = msg;
								}
							}
							if (debug) {
								System.out.println(translatedText);
							}
							bw.write(translatedText);
						}
						bw.write("\r\n");
					}
					bw.close();
					bw = null;
					System.out.println();
				} catch (IOException e) {
					System.out.println(e.getMessage());
				} finally {
					if (bw != null) {
						bw.close();
					}
				}
			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
			if (fis != null) {
				try {
					fis.close();
				} catch (IOException e1) {
				}
			}
			if (bw != null) {
				try {
					bw.close();
				} catch (IOException e1) {
				}
			}
		}
	}
}
